package com.xingji.caixia;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import com.idescout.sql.SqlScoutServer;
import com.tencent.bugly.crashreport.CrashReport;
import com.xingji.caixia.entitys.CardDao;
import com.xingji.caixia.entitys.UploadData;

import org.xutils.DbManager;
import org.xutils.db.table.TableEntity;
import org.xutils.ex.DbException;
import org.xutils.x;

/**
 * Created by zyh on 2017/9/19.
 */

public class MyApplication extends Application {

    public static DbManager db ;
    public static MyApplication instance;
    public static MyApplication getInstance(){
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        x.Ext.init(this);
        x.Ext.setDebug(true);
//        CrashReport.initCrashReport(this,"147b5a794e",true);

        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                .setDbName("licaixia")
                .setDbVersion(2)
                //设置数据库打开的监听
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        //开启数据库支持多线程操作，提升性能，对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                //设置数据库更新的监听
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                        Log.e("onUpgrade",oldVersion+"--"+newVersion);
                        try {
                            db.addColumn(UploadData.class,"result");
                            db.addColumn(UploadData.class,"issad");
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                    }
                })
                //设置表创建的监听
                .setTableCreateListener(new DbManager.TableCreateListener() {
                    @Override
                    public void onTableCreated(DbManager db, TableEntity<?> table){
                        Log.e("onTableCreated", "onTableCreated：" + table.getName());
                    }
                });
        //设置是否允许事务，默认true
        //.setAllowTransaction(true)


        db = x.getDb(daoConfig);
        SqlScoutServer.create(this,getPackageName());
    }

    public String getVersionName() {
        // 获取packagemanager的实例
        PackageManager packageManager =  getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(getPackageName(),0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return "";
        }
        String version = packInfo.versionName;
        return version;
    }
}
